package com.zelma.agent.controller;


import com.zelma.agent.feign.ConsumerFeignService;
import com.zelma.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zelma.agent.entity.GroupbuyEntity;
import com.zelma.agent.service.GroupbuyService;

import java.io.IOException;
import java.util.List;


/**
 * 
 *
 * @author zelma
 * @email 1239115389@qq.com
 * @date 2022-01-06 16:48:57
 */
@RestController
@RequestMapping("agent/groupbuy")
@SuppressWarnings("all")
public class GroupbuyController {

    @Autowired
    private GroupbuyService groupbuyService;

    @Autowired
    private ConsumerFeignService customerFeign;

    @PostMapping("/eslist")
    public void eslist() throws IOException {

        groupbuyService.eslist();
    }

    @DeleteMapping("/deleteByGroupBuyId/{groupbuyId}")
    public R deleteByGroupBuyId(@PathVariable("groupbuyId") Integer groupbuyId){

        Integer i = groupbuyService.deleteByGroupBuyId(groupbuyId);

        if (i>0){
            return R.ok();
        }

        return R.error(500,"error");
    }

    //团购管理查询接口
    @GetMapping("/findGroupBuyInfo")
    public R findGroupBuyInfo(@RequestParam(value = "groupbuyTitle",defaultValue = "") String groupbuyTitle,
                              @RequestParam(value = "groupbuyWhetherStatus",defaultValue = "") Integer groupbuyWhetherStatus,
                              @RequestParam(value = "groupbuyShowStatus",defaultValue = "") Integer groupbuyShowStatus,
                              @RequestParam(value = "cname",defaultValue = "") String cname) throws Exception {

        List <GroupbuyEntity> list = groupbuyService.findGroupBuyInfo(groupbuyTitle, groupbuyWhetherStatus, groupbuyShowStatus, cname);

        return R.ok().put("list",list);
    }

//    客户端小程序-我要报名接口(前提条件)
    @PostMapping("/eroll/{groupbuyId}/{userId}")
    public R Eroll(@PathVariable("groupbuyId")Integer groupbuyId,
                   @PathVariable("userId")Integer userId){
        GroupbuyEntity byId = groupbuyService.getById(groupbuyId);
        //    在进入报名表前 首先进行判断团购项目 是否是展示状态
        if(byId.getGroupbuyShowStatus().equals(1)){
//            不展示状态 不可报名
            return R.error().put("msg","团购项目处于不可展示状态 无法进行报名操作");
        }

//        判断是否开团  如果已开团 则不可以再加人
        if(byId.getGroupbuyWhetherStatus().equals(0)){
            return R.error().put("msg","该项目已经开团,请选择其余项目");
        }

//        两者都满足 则进入报名表操作
        byId.setGroupbuyNum(byId.getGroupbuyNum()+1);
        groupbuyService.updateById(byId);
//        Feign调用
        return customerFeign.Enlist(userId,groupbuyId);
    }

//    根据团购id查询团购报名人数
    @GetMapping("/groupsignNum")
    public R GroupSignNum(@RequestParam("groupbuyId")Integer groupbuyId){
        int i = groupbuyService.groupSignNum(groupbuyId);
        return R.ok().put("code",i);
    }


}
